﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SysShopBuyCard
    {
        public int Add(Chain.Model.SysShopBuyCard model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SysShopBuyCard(");
            builder.Append("StartCardNumber,EndCardNumber,BuyCardShopid,UserID,Remark,BuyCardTime,BuyCardMoney)");
            builder.Append(" values (");
            builder.Append("@StartCardNumber,@EndCardNumber,@BuyCardShopid,@UserID,@Remark,@BuyCardTime,@BuyCardMoney)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StartCardNumber", SqlDbType.NVarChar, 200), new SqlParameter("@EndCardNumber", SqlDbType.NVarChar, 200), new SqlParameter("@BuyCardShopid", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Remark", SqlDbType.NVarChar, 200), new SqlParameter("@BuyCardTime", SqlDbType.DateTime), new SqlParameter("@BuyCardMoney", SqlDbType.Money, 8) };
            cmdParms[0].Value = model.StartCardNumber;
            cmdParms[1].Value = model.EndCardNumber;
            cmdParms[2].Value = model.BuyCardShopid;
            cmdParms[3].Value = model.UserID;
            cmdParms[4].Value = model.Remark;
            cmdParms[5].Value = model.BuyCardTime;
            cmdParms[6].Value = model.BuyCardMoney;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int BuyCardID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysShopBuyCard ");
            builder.Append(" where BuyCardID=@BuyCardID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuyCardID", SqlDbType.Int, 4) };
            cmdParms[0].Value = BuyCardID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string BuyCardIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SysShopBuyCard ");
            builder.Append(" where BuyCardID in (" + BuyCardIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int BuyCardID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SysShopBuyCard");
            builder.Append(" where BuyCardID=@BuyCardID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuyCardID", SqlDbType.Int, 4) };
            cmdParms[0].Value = BuyCardID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select BuyCardID,StartCardNumber,EndCardNumber,BuyCardShopid,UserID,Remark,BuyCardTime,BuyCardMoney ");
            builder.Append(" FROM SysShopBuyCard ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" BuyCardID,StartCardNumber,EndCardNumber,BuyCardShopid,UserID,Remark,BuyCardTime,BuyCardMoney ");
            builder.Append(" FROM SysShopBuyCard ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.BuyCardID desc");
            }
            builder.Append(")AS Row, T.*  from SysShopBuyCard T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, string[] strWhere, out int resCount)
        {
            string tableName = "SysShopBuyCard,SysShop,SysUser";
            string[] column = new string[] { "SysShopBuyCard.*,SysShop.ShopName,SysUser.UserName" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "BuyCardID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public Chain.Model.SysShopBuyCard GetModel(int BuyCardID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 BuyCardID,StartCardNumber,EndCardNumber,BuyCardShopid,UserID,Remark,BuyCardTime,BuyCardMoney from SysShopBuyCard ");
            builder.Append(" where BuyCardID=@BuyCardID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuyCardID", SqlDbType.Int, 4) };
            cmdParms[0].Value = BuyCardID;
            Chain.Model.SysShopBuyCard card = new Chain.Model.SysShopBuyCard();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["BuyCardID"] != null) && (set.Tables[0].Rows[0]["BuyCardID"].ToString() != ""))
            {
                card.BuyCardID = int.Parse(set.Tables[0].Rows[0]["BuyCardID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["StartCardNumber"] != null) && (set.Tables[0].Rows[0]["StartCardNumber"].ToString() != ""))
            {
                card.StartCardNumber = set.Tables[0].Rows[0]["StartCardNumber"].ToString();
            }
            if ((set.Tables[0].Rows[0]["EndCardNumber"] != null) && (set.Tables[0].Rows[0]["EndCardNumber"].ToString() != ""))
            {
                card.EndCardNumber = set.Tables[0].Rows[0]["EndCardNumber"].ToString();
            }
            if ((set.Tables[0].Rows[0]["BuyCardShopid"] != null) && (set.Tables[0].Rows[0]["BuyCardShopid"].ToString() != ""))
            {
                card.BuyCardShopid = int.Parse(set.Tables[0].Rows[0]["BuyCardShopid"].ToString());
            }
            if ((set.Tables[0].Rows[0]["UserID"] != null) && (set.Tables[0].Rows[0]["UserID"].ToString() != ""))
            {
                card.UserID = int.Parse(set.Tables[0].Rows[0]["UserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["Remark"] != null) && (set.Tables[0].Rows[0]["Remark"].ToString() != ""))
            {
                card.Remark = set.Tables[0].Rows[0]["Remark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["BuyCardTime"] != null) && (set.Tables[0].Rows[0]["BuyCardTime"].ToString() != ""))
            {
                card.BuyCardTime = DateTime.Parse(set.Tables[0].Rows[0]["BuyCardTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["BuyCardMoney"] != null) && (set.Tables[0].Rows[0]["BuyCardMoney"].ToString() != ""))
            {
                card.BuyCardMoney = decimal.Parse(set.Tables[0].Rows[0]["BuyCardMoney"].ToString());
            }
            return card;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SysShopBuyCard ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.SysShopBuyCard model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SysShopBuyCard set ");
            builder.Append("StartCardNumber=@StartCardNumber,");
            builder.Append("EndCardNumber=@EndCardNumber,");
            builder.Append("BuyCardShopid=@BuyCardShopid,");
            builder.Append("UserID=@UserID,");
            builder.Append("Remark=@Remark,");
            builder.Append("BuyCardTime=@BuyCardTime,");
            builder.Append("BuyCardMoney=@BuyCardMoney");
            builder.Append(" where BuyCardID=@BuyCardID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@StartCardNumber", SqlDbType.NVarChar, 200), new SqlParameter("@EndCardNumber", SqlDbType.NVarChar, 200), new SqlParameter("@BuyCardShopid", SqlDbType.Int, 4), new SqlParameter("@UserID", SqlDbType.Int, 4), new SqlParameter("@Remark", SqlDbType.NVarChar, 200), new SqlParameter("@BuyCardTime", SqlDbType.DateTime), new SqlParameter("@BuyCardMoney", SqlDbType.Money, 8), new SqlParameter("@BuyCardID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.StartCardNumber;
            cmdParms[1].Value = model.EndCardNumber;
            cmdParms[2].Value = model.BuyCardShopid;
            cmdParms[3].Value = model.UserID;
            cmdParms[4].Value = model.Remark;
            cmdParms[5].Value = model.BuyCardTime;
            cmdParms[6].Value = model.BuyCardMoney;
            cmdParms[7].Value = model.BuyCardID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

